package org.base23.video.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.base23.commons.domain.dto.PageQueryParam;
import org.base23.commons.domain.dto.PageResult;
import org.base23.video.domain.dto.CreateAnalysisResultParam;
import org.base23.video.domain.dto.UpdateAnalysisResultParam;
import org.base23.video.domain.dto.UserAnalysisResultDTO;
import org.base23.video.service.UserAnalysisResultService;
import org.base23.web.model.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户分析结果控制器
 */
@RestController
@RequestMapping("/user-analysis")
@Tag(name = "用户分析结果管理", description = "用户分析结果的增删改查操作")
@RequiredArgsConstructor
@Slf4j
public class UserAnalysisResultController {

    private final UserAnalysisResultService userAnalysisResultService;

    @PostMapping
    @Operation(summary = "创建分析结果", description = "为用户创建一个新的分析结果记录")
    public Result createAnalysisResult(@Valid @RequestBody CreateAnalysisResultParam param) {
        log.info("创建分析结果请求，用户ID: {}, 结果ID: {}", param.getUserId(), param.getResultId());
        Long id = userAnalysisResultService.createAnalysisResult(param);
        return Result.data(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询分析结果", description = "根据分析结果ID查询详细信息")
    public Result getAnalysisResultById(
            @Parameter(description = "分析结果ID") @PathVariable Long id) {
        log.info("查询分析结果，ID: {}", id);
        UserAnalysisResultDTO result = userAnalysisResultService.getAnalysisResultById(id);
        return Result.data(result);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查询用户的所有分析结果", description = "根据用户ID查询该用户的所有分析结果")
    public Result getAnalysisResultsByUserId(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        log.info("查询用户分析结果，用户ID: {}", userId);
        List<UserAnalysisResultDTO> results = userAnalysisResultService.getAnalysisResultsByUserId(userId);
        return Result.data(results);
    }

    @GetMapping("/user/{userId}/page")
    @Operation(summary = "分页查询用户分析结果", description = "根据用户ID分页查询分析结果")
    public Result pageQueryByUserId(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "分页参数") PageQueryParam pageParam) {
        log.info("分页查询用户分析结果，用户ID: {}, 页码: {}, 页大小: {}", 
                userId, pageParam.getPageNo(), pageParam.getPageSize());
        PageResult<UserAnalysisResultDTO> results = userAnalysisResultService.pageQueryByUserId(userId, pageParam);
        return Result.data(results);
    }

    @GetMapping("/user/{userId}/sport/{sportType}")
    @Operation(summary = "根据运动类型查询分析结果", description = "根据用户ID和运动类型查询分析结果")
    public Result getAnalysisResultsByUserIdAndSportType(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "运动类型") @PathVariable String sportType) {
        log.info("查询用户特定运动类型分析结果，用户ID: {}, 运动类型: {}", userId, sportType);
        List<UserAnalysisResultDTO> results = userAnalysisResultService.getAnalysisResultsByUserIdAndSportType(userId, sportType);
        return Result.data(results);
    }

    @GetMapping("/user/{userId}/result/{resultId}")
    @Operation(summary = "根据用户ID和结果ID查询", description = "根据用户ID和分析结果ID查询特定的分析结果")
    public Result getAnalysisResultByUserIdAndResultId(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "分析结果ID") @PathVariable String resultId) {
        log.info("查询用户特定分析结果，用户ID: {}, 结果ID: {}", userId, resultId);
        UserAnalysisResultDTO result = userAnalysisResultService.getAnalysisResultByUserIdAndResultId(userId, resultId);
        return Result.data(result);
    }

    @PutMapping
    @Operation(summary = "更新分析结果", description = "更新分析结果的信息")
    public Result updateAnalysisResult(@Valid @RequestBody UpdateAnalysisResultParam param) {
        log.info("更新分析结果请求，ID: {}", param.getId());
        userAnalysisResultService.updateAnalysisResult(param);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分析结果", description = "根据ID删除分析结果")
    public Result deleteAnalysisResult(@Parameter(description = "分析结果ID") @PathVariable Long id) {
        log.info("删除分析结果请求，ID: {}", id);
        userAnalysisResultService.deleteAnalysisResult(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除分析结果", description = "批量删除多个分析结果")
    public Result batchDeleteAnalysisResults(@RequestBody List<Long> ids) {
        log.info("批量删除分析结果请求，数量: {}", ids.size());
        userAnalysisResultService.batchDeleteAnalysisResults(ids);
        return Result.success();
    }

    @GetMapping("/{id}/ownership/{userId}")
    @Operation(summary = "检查所有权", description = "检查分析结果是否属于指定用户")
    public Result checkOwnership(
            @Parameter(description = "分析结果ID") @PathVariable Long id,
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        log.info("检查分析结果所有权，ID: {}, 用户ID: {}", id, userId);
        boolean isOwner = userAnalysisResultService.checkOwnership(id, userId);
        return Result.data(isOwner);
    }
} 